<?php

abstract class DataXActivity extends SyncActivity {
	
	protected $input;
	protected $output;
	protected $resourceService;
	
	public function __construct($activity) {
   		parent::__construct($activity);  
   		$this->input = $this->activity['inputs'][0];
		$this->output = $this->activity['outputs'][0];    
    }
    
	protected function prepare() {
    	if(!parent::prepare()) {
    		return false;
    	}
    	
    	$this->cmd = PYTHON_EXEC . ' ' . DATAX_CLIENT . "/{$this->cmd}";
		return true;
    }
    
    protected function prepareInputs() {
    	return true;
    }
    
    protected function prepareOutputs() {
    	return true;
    }
    
	protected function checkDepends() {
		// Check the depend resource partition is ready
		if(!parent::checkDepends()) {
			return false;
		}
		
		$status = $this->resourceService->checkResourcePartition($this->input['resource']['physical']['physical_id'], SpecialPartition::None);
		
		if(!$status) {
			$this->setRuntimeInfo(array(
				'state'	=>	TaskState::Waiting,
				'message'	=>	"[DataX/checkDepend]physical resource[{$this->input['resource']['physical']['physical_name']}] is not ready",
			));
			Yii::log("physical resource[{$this->input['resource']['physical']['physical_name']}] is not ready", CLogger::LEVEL_ERROR, WORKER_LOG);
			return false;
		}
		
		return true;
	}
	
	protected function save() {	
		if(!parent::save()) {
			return false;
		}
			
		Yii::log("save output resource:[{$this->output['resource']['res_name']}]", CLogger::LEVEL_INFO, WORKER_LOG);
		$partition = $this->resourceService->getResourcePartition($this->input['resource']['physical']['physical_id'], SpecialPartition::None);
		
		// Save the resource partition.
		$partitionRecord = array(
			'physical_id'	=>	$this->output['resource']['physical']['physical_id'],
			'partition'		=>	SpecialPartition::None,
			'size'			=>	$partition['size'],
			'count'			=>	$partition['count'],
			'schema'		=>	'',
			'create_time'	=>	date('Y-m-d H:i:s'),
		); 
		$partitionId = $this->resourceService->addResourcePartition($partitionRecord);
		if($partitionId === false) {
			$this->setRuntimeInfo(array(
				'state'	=>	TaskState::Failed,
				'message'	=>	"add resource[{$this->output['resource']['res_name']}] partition failed",
			));
			Yii::log("add resource[{$this->output['resource']['res_name']}] partition failed", CLogger::LEVEL_ERROR, WORKER_LOG);
			return false;
		}
	
		return true;
	}
}